package com.ningjingjing.dao;


import com.ningjingjing.model.Album;

import java.sql.*;

public class AlbumDao {
    public Album selectOneByAid(Connection c, int aid) throws SQLException {
        String sql = "SELECT * FROM album WHERE aid = ?";
        try (PreparedStatement s = c.prepareStatement(sql)) {
            s.setInt(1, aid);

            try (ResultSet rs = s.executeQuery()) {
                if (!rs.next()) {
                    return null;
                }

                Album album = new Album();
                album.aid  = aid;
                album.name = rs.getString("name");
                album.cover = rs.getString("cover");

                return album;
            }
        }
    }

    public int insert(Connection c, int uid, String name, String cover) throws SQLException {
        String sql = "INSERT INTO album (uid, name, cover) VALUES (?, ?, ?)";

        try (PreparedStatement s = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            s.setInt(1, uid);
            s.setString(2, name);
            s.setString(3, cover);

            s.executeUpdate();

            try (ResultSet rs = s.getGeneratedKeys()) {
                rs.next();

                return rs.getInt(1);
            }
        }
    }
}
